package 预科0103Poker;
import java.util.Arrays;
/*（1）首先准备 54 张扑克牌并打乱顺序。 

（2）由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌。 

（3）查看三个玩家手中的扑克牌和底牌,扑克牌需要按照大小顺序打印*/
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
public class PokerGame implements GameAction{
	private static HashMap<Integer,PokerCard> pokercards=new HashMap<Integer,PokerCard>();
	private static HashMap<Integer,PokerCard> player1=new HashMap<Integer,PokerCard>();
	private static HashMap<Integer,PokerCard> player2=new HashMap<Integer,PokerCard>();
	private static HashMap<Integer,PokerCard> player3=new HashMap<Integer,PokerCard>();
	private static HashMap<Integer,PokerCard> leftcards=new HashMap<Integer,PokerCard>();
	private static PokerGame pokergame=new PokerGame();
	private PokerGame() {
	}
	
	public static PokerGame getPokerGame() {
		return pokergame;
	}
	
	//准备 54 张扑克牌并打乱顺序
	@Override
	public void shuffle() {
		//54张牌按序放入
		int i=0;
		for(int j=1;j<14;j++) {
				pokercards.put(--i,new PokerCard(0,j));
				pokercards.put(--i,new PokerCard(1,j));
				pokercards.put(--i,new PokerCard(2,j));
				pokercards.put(--i,new PokerCard(3,j));
			}
		pokercards.put(--i,new PokerCard(4,14));
		pokercards.put(--i,new PokerCard(4,15));
		
		int key;
		//打乱牌序
		Random r=new Random();
		for(int j=-1;j>=-54;j--) {
			do{
				//0-53的随机整数
				key=r.nextInt(54);
			}while(pokercards.containsKey(key));
			pokercards.put(key,pokercards.get(j)); 
		}
	}
	//由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌
	@Override
	public void deal() {
		//三个玩家交替摸牌，每人 17 张扑克牌
		for(int j=1;j<18;j++) {
			player1.put(j,pokercards.get((j-1)*3)); 
			player2.put(j,pokercards.get((j-1)*3+1)); 
			player3.put(j,pokercards.get((j-1)*3+2)); 
		}
		//最后三张留作底牌
		leftcards.put(1,pokercards.get(51)); 
		leftcards.put(2,pokercards.get(52)); 
		leftcards.put(3,pokercards.get(53)); 
	}
	//查看三个玩家手中的扑克牌和底牌,扑克牌需要按照大小顺序打印
	@Override
	public void show() {
		//扑克牌按照大小顺序排序
		sort(player1);
		sort(player2);
		sort(player3);
		sort(leftcards);
		//遍历打印
		for(int i:player1.keySet()) {
			System.out.println("玩家1的第"+i+"张牌为"+player1.get(i).toString());
		}
		for(int i:player2.keySet()) {
			System.out.println("玩家2的第"+i+"张牌为"+player2.get(i).toString());
		}
		for(int i:player3.keySet()) {
			System.out.println("玩家3的第"+i+"张牌为"+player3.get(i).toString());
		}
		for(int i:leftcards.keySet()) {
			System.out.println("底牌的第"+i+"张牌为"+leftcards.get(i).toString());
		}
		
	}
	//扑克牌按照大小顺序排序
	@Override
	public void sort(HashMap<Integer,PokerCard> cards) {
		for(int i:cards.keySet()) {
			for(int j:cards.keySet()) {
				if(cards.get(j).getValue()<cards.get(i).getValue()) {
					PokerCard card=cards.get(i);
					cards.put(i,cards.get(j));
					cards.put(j,card);
				};
			}
		}
		
	}


	
}
 